﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using MixScriptWeb.Share;
using MixScriptWeb.control;
using MixScript.Appl.Message;
using MixScript.BLL;
using MixScript.Appl.Session;
namespace MixScriptWeb
{

    public class cAccCommon : Page
    {
        private Panel pnlStatus;
        private Label lbmsg;
        public static AccMaster mn;
        public string errMsg = "";
        public ErrMessage em = new ErrMessage();

        public cAccCommon()
        {
            mn = new AccMaster();
            pnlStatus = (Panel)mn.FindControl("statusbar");
            lbmsg = (Label)mn.FindControl("lblError");
        }


        public string getUserLogin()
        {

            return new cApplSession().userID;
        }

        public void initGvColumn(GridView gv, string[] headerText, string[] datavalue)
        {
            try
            {
                if (headerText.Length != datavalue.Length)
                {
                    throw new Exception("Header text length not equal with datavalue length");
                }
                int i = 0;
                foreach (string s in headerText)
                {
                    BoundField bf = new BoundField();
                    bf.HeaderText = s;



                    bf.DataField = datavalue[i];
                    bf.SortExpression = datavalue[i];
                    i++;
                    gv.Columns.Add(bf);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void initGvColumn(GridView gv, string[] headerText, string[] datavalue, int[] indxformat, string strformat)
        {
            try
            {
                if (headerText.Length != datavalue.Length)
                {
                    throw new Exception("Header text length not equal with datavalue length");
                }
                int i = 0;
                foreach (string s in headerText)
                {
                    BoundField bf = new BoundField();
                    bf.HeaderText = s;


                    if (indxformat.Contains(i))
                    {
                        bf.DataFormatString = strformat;
                        bf.HtmlEncode = false;
                    }

                    bf.DataField = datavalue[i];
                    bf.SortExpression = datavalue[i];
                    i++;
                    gv.Columns.Add(bf);
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void FillListBox(ListBox dl, string tablename, string stext, string sval, bool withempty)
        {

            try
            {
                DataTable dt = new DataTable();
                cReferences oref = new cReferences();
                if (oref.getComboboxdata(tablename, stext, sval, ref dt))
                {
                    dl.DataTextField = stext;
                    dl.DataValueField = sval;
                    dl.DataSource = dt;
                    dl.DataBind();
                    if (withempty)
                    {
                        //dl.AppendDataBoundItems
                    }

                }


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void FillDropDownList(DropDownList dl, string tablename, string stext, string sval, bool withempty)
        {

            try
            {
                DataTable dt = new DataTable();
                cReferences oref = new cReferences();
                if (oref.getComboboxdata(tablename, stext, sval, ref dt))
                {
                    dl.DataTextField = stext;
                    dl.DataValueField = sval;
                    dl.DataSource = dt;
                    dl.DataBind();
                    if (withempty)
                    {
                        //dl.AppendDataBoundItems
                    }

                }


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool IntegerOnly(bool allowminus, Control[] ct, out string sctrlname)
        {
            sctrlname = "";
            int iint = 0;
            foreach (Control c in ct)
            {
                if (c.GetType().Name == "control_mixscripttb_ascx")
                {

                    MixScriptTB mx = (MixScriptTB)c;
                    if (!int.TryParse(mx.Text, out iint))
                    {
                        sctrlname = c.ID;
                        return false;
                    }
                    else
                    {
                        if (!allowminus)
                        {
                            sctrlname = c.ID;
                            return iint >= 0;
                        }
                        return true;
                    }

                }
                else
                {
                    TextBox t = (TextBox)c;
                    if (!int.TryParse(t.Text, out iint))
                    {
                        sctrlname = c.ID;
                        return false;
                    }
                    else
                    {
                        if (!allowminus)
                        {
                            sctrlname = c.ID;
                            return iint >= 0;
                        }
                        return true;
                    }
                }

            }
            return true;
        }



        public bool MustFilledOk(Control[] ct, out string sctrlname)
        {
            try
            {
                sctrlname = "";
                foreach (Control c in ct)
                {
                    if (c.GetType().Name == "control_mixscripttb_ascx")
                    {
                        MixScriptTB mx = (MixScriptTB)c;
                        if (mx.Text.Trim() == string.Empty)
                        {
                            sctrlname = c.ID;
                            return false;
                        }

                    }
                    else
                    {
                        TextBox t = (TextBox)c;
                        if (t.Text.Trim() == string.Empty)
                        {
                            sctrlname = c.ID;
                            return false;

                        }
                    }

                }
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public string[] genInqParam(Control[] ct)
        {
            try
            {
                string[] ss = new string[ct.Length];
                string s = "";
                int i = 0;
                foreach (Control c in ct)
                {
                    if (c == null)
                    {
                        ss[i] = string.Empty;
                        i++;
                    }
                    else
                    {
                        if (c.GetType().Name == "control_mixscripttb_ascx")
                        {
                            MixScriptTB mx = (MixScriptTB)c;
                            if (mx.Text.Trim() != string.Empty)
                            {
                                if (mx.InquiryMethod == InqMethod.Equal)
                                {
                                    if (mx.UseQuote)
                                    {
                                        s = "'" + mx.Text + "'";
                                    }
                                    else
                                    {
                                        s = mx.Text;
                                    }
                                }
                                else if (mx.InquiryMethod == InqMethod.Like)
                                {
                                    s = "%" + mx.Text + "%";
                                }

                            }
                        }
                        else if (c.GetType().Name == "DropDownList")
                        {
                            DropDownList dl = (DropDownList)c;
                            if (dl.SelectedValue != "")
                            {
                                s = "'" + dl.SelectedValue + "'";
                            }
                        }
                        else if (c.GetType().Name == "TextBox")
                        {
                            TextBox tb = (TextBox)c;
                            if (tb.Text != string.Empty)
                            {

                                if (tb.ID.ToLower().Contains("date"))
                                {
                                    s = tb.Text.Substring(6, 4) + "-" + tb.Text.Substring(3, 2) + "-" + tb.Text.Substring(0, 2);
                                }
                                else
                                {
                                    s = "'" + tb.Text + "'";
                                }

                            }


                        }
                        else
                        {
                        }
                        ss[i] = s;
                        s = string.Empty;
                        i++;
                    }
                }

                return ss;



            }
            catch (Exception ex)
            {

                throw;
            }
        }

        public void setColumnVisibility(GridView gv, int[] idxCol, bool bstate, int istart)
        {
            try
            {
                foreach (int i in idxCol)
                {
                    gv.Columns[i + istart].Visible = bstate;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public string ShortDirection(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }


        public void setSortExpression(GridView gv, string[] arrSortExp)
        {
            try
            {
                setSortExpression(gv, arrSortExp, 1);
            }
            catch (Exception ex)
            {

                //   throw ex;
            }

        }
        public void setSortExpression(GridView gv, string[] arrSortExp, int idxStart)
        {
            try
            {
                int i = 0;
                foreach (string s in arrSortExp)
                {
                    if (s != string.Empty)
                    {
                        gv.Columns[i + idxStart].SortExpression = s;
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {

                //  throw;
            }

        }

        public void bindGridview(GridView gv, DataView dv)
        {
            try
            {
                gv.DataSource = dv;
                gv.DataBind();


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void bindGridview(GridView gv, DataView dv, LinkButton lb)
        {
            try
            {
                gv.DataSource = dv;
                gv.DataBind();
                lb.Visible = dv.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void bindGridview(GridView gv, DataTable dt, LinkButton lb)
        {
            try
            {
                gv.DataSource = dt;
                gv.DataBind();
                lb.Visible = dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void bindGridview(GridView gv, DataTable dt)
        {
            try
            {
                gv.DataSource = dt;
                gv.DataBind();
                //lb.Visible = dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void setValid(MixScriptTB[] mtb)
        {
            foreach (MixScriptTB m in mtb)
            {
                m.Text = m.Text;
                m.isvalid = true;
            }
        }

        void initStatusbar()
        {
            if (pnlStatus == null)
            {
                mn = new AccMaster();
                pnlStatus = (Panel)mn.FindControl("statusbar");
                lbmsg = (Label)mn.FindControl("lblError");
            }
        }
        public void addConfirmation(Page p, Button[] bt, string[] confText)
        {
            try
            {
                int i = 0;
                if (bt.Length != confText.Length)
                    throw new Exception("The number button must in the same with confirmation text");
                foreach (Button m in bt)
                {
                    m.OnClientClick = "return confirm('Are You sure want to Delete this record ?')";
                    i++;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        public void addRead(MixScriptTB[] mtb)
        {
            try
            {

                int i = 0;


                foreach (MixScriptTB m in mtb)
                {
                    TextBox im = (TextBox)m.FindControl("txt");
                    im.Attributes.Add("readonly", "readonly");
                    i++;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void addFindClickAttribute_COA(Page p, MixScriptTB[] mtb, Label[] lb)
        {
            try
            {
                int i = 0;


                foreach (MixScriptTB m in mtb)
                {
                    System.Web.UI.WebControls.Image im = (System.Web.UI.WebControls.Image)m.FindControl("imgFind");
                    im.Attributes.Add("onclick", "return browseCOA('" + p.Form.ClientID + "', '" + m.ClientID + "_txt','" + m.ClientID + "_lbl','" + lb[i].ClientID + "','" + m.ClientID + "_hf" + "');");
                    i++;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        public void addFindClickAttribute(Page p, MixScriptTB[] mtb, string[] searchid)
        {
            try
            {
                int i = 0;
                if (mtb.Length != searchid.Length)
                    throw new Exception("The number searchid must in the same with control");
                foreach (MixScriptTB m in mtb)
                {
                    System.Web.UI.WebControls.Image im = (System.Web.UI.WebControls.Image)m.FindControl("imgFind");
                    im.Attributes.Add("onclick", "return browseitem('" + p.Form.ClientID + "', '" + m.ClientID + "_txt','" + m.ClientID + "_lbl','" + searchid[i] + "');");
                    i++;

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public void hideStatusBar()
        {
            initStatusbar();
            if (pnlStatus != null)
            {
                pnlStatus.Visible = false;
            }
        }
        public void ShowStatusBar(string statusbarmsg)
        {
            ShowStatusBar(statusbarmsg, msgType.msgError);
        }
        public void ShowStatusBar(string statusbarmsg, msgType messageType)
        {
            try
            {
                if (mn == null)
                {
                    mn = new AccMaster();
                }
                initStatusbar();
                lbmsg.Text = statusbarmsg;
                if (messageType == msgType.msgError)
                {
                    pnlStatus.CssClass = "error";
                }
                else if (messageType == msgType.msgSuccess)
                {
                    pnlStatus.CssClass = "success";
                }
                else if (messageType == msgType.msgWarning)
                {
                    pnlStatus.CssClass = "warning";
                }
                else if (messageType == msgType.msgTip)
                {
                    pnlStatus.CssClass = "tip";
                }
                pnlStatus.Visible = true;

            }
            catch (Exception ex)
            {

               // throw ex;
            }
        }

        public void removeNbsp(MixScriptTB[] mtb)
        {
            try
            {
                foreach (MixScriptTB m in mtb)
                {
                    if (m.Text.ToLower() == "&nbsp;")
                        m.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void setControlVisibility(Control[] ct, bool isvisible)
        {
            try
            {
                foreach (Control c in ct)
                {
                    c.Visible = isvisible;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void clearMixTB(MixScriptTB[] mtb)
        {
            try
            {
                foreach (MixScriptTB m in mtb)
                {
                    m.Text = "";
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void ClearTextbox(TextBox[] tb)
        {
            try
            {
                foreach (TextBox t in tb)
                {
                    t.Text = string.Empty;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        public void setLabelColor(Label[] lb, Color cl)
        {
            try
            {
                foreach (Label l in lb)
                {
                    l.ForeColor = cl;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
    }
}